<template>
  <div class="userBox">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/mamger' }"
        >商品管理</el-breadcrumb-item
      >
      <el-breadcrumb-item :to="{ path: '/users' }">商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- @blur="getSearchUsers" -->
    <el-card class="box-card">
      <el-row type="flex">
        <el-col :span="12">
          <div class="grid-content bg-purple title" style="text-align: left">
            <el-input
              clearable
              placeholder="请输入内容"
              v-model.trim="params.query"
              class="input-with-select"
              @clear="getSearchUsers"
              @keydown.native.enter="getSearchUsers"
            >
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="getSearchUsers"
              ></el-button>
            </el-input>
          </div>
        </el-col>

        <el-col :span="12">
          <el-row type="flex">
            <el-col :span="2"></el-col>
            <el-col :span="20">
              <el-button type="primary" @click="dialogFormVisible = true"
                >添加商品</el-button
              >
            </el-col>
          </el-row>
        </el-col>
      </el-row>

      <el-table :data="tableData" border style="width: 100%  margin-top:10px">
        <el-table-column type="index" width="50"> </el-table-column>

        <el-table-column prop="goods_name" label="商品名称" width="600">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格" width="100">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100">
        </el-table-column>
        <el-table-column prop="upd_time" label="创建时间" width="120">
        </el-table-column>
        <!-- prop="mg_state" -->
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row.goods_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!--  分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="params.pagenum"
      :page-sizes="[1, 2, 5, 10]"
      :page-size="params.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalpage"
    >
    </el-pagination>

    <!--  添加商品弹框 -->
    <el-dialog title="添加商品" :visible.sync="dialogFormVisible">
      <el-form :model="form" ref="addform" :rules="rules">
        <el-form-item
          label="商品名称"
          prop="goods_name"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.goods_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="商品价格"
          prop="goods_price"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.goods_price" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="商品重量"
          prop="goods_weight"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.goods_weight" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="创建时间"
          prop="upd_time"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.upd_time" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addGoods">确 定</el-button>
      </div>
    </el-dialog>

    <!--  编辑用户弹框 -->

    <el-dialog title="编辑用户" :visible.sync="editeDialogFormVisible">
      <el-form :model="editeForm" ref="form" :rules="rules">
        <el-form-item
          label="商品名称"
          prop="goods_name"
          :label-width="formLabelWidth"
        >
          <el-input v-model="editeForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="商品价格"
          prop="goods_price"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="editeForm.password"
            disabled
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="商品重量"
          prop="goods_weight"
          :label-width="formLabelWidth"
        >
          <el-input v-model="editeForm.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="创建时间"
          prop="upd_time"
          :label-width="formLabelWidth"
        >
          <el-input v-model="editeForm.mobile" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="editeDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUsers">编辑确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getUsers,
  getChangeUserState,
  getAddGoods,
  getDeleteGoods,
  getQueryUsersById,
  getEditUsers,
  getGoods,
  getUsersRole,
  getRoles,
} from "../../api/index";

export default {
  data() {
    return {
         role_id: 0, // 定义角色id
      tableData: [],
      totalpage: 0, //总页数
      params: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      //分页

      // 定义弹框数据

      dialogFormVisible: false,
      form: {
        goods_name: "",
        goods_price: "",
        goods_weight: "",
        upd_time: "",
      },
      formLabelWidth: "200px",
      //编辑用户数据定义
      editeDialogFormVisible: false,
      editeForm: {
        goods_name: "",
        goods_price: "",
        goods_weight: "",
        upd_time: "",
      },
      rules: {
        goods_name: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
        ],
         goods_price: [
          { required: true, message: "请输入用户密码", trigger: "blur" },
        ],
      },
    };
  },

  created() {
    this.getGoodsList();
  },
  methods: {
    //初始化获取用户列表
    async getGoodsList() {
      let {
        data: { goods, pagenum, total },
      } = await getGoods(this.params);

      this.tableData = goods;
      this.params.pagenum = +pagenum;
      this.totalpage = total;
    },
    //分页功能
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.params.pagesize = val;
      this.getGoodsList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.params.pagenum = val;
      this.getGoodsList();
    },

    //搜索功能
    getSearchUsers() {
      this.getGoodsList();
    },
  //添加用户逻辑
    async addGoods() {
      //   1 表单的验证
      //   2 验证通过后 发送请求
      //   3 发送请求成功后关闭弹框  重新刷新页面

      //  1 表单的验证
      let success = await this.$refs["addform"].validate().catch(() => {});
      console.log(success);

      if (!success) return false;
      // 2 验证通过后 发送请求
      console.log(this.form)
      let {
        meta: { status },
      } = await getAddGoods(this.form);
  console.log(status)
      if (status == 201) {
        this.$message.success("添加用户成功");
        //关闭弹框
        this.dialogFormVisible = false;
        // 重新刷新页面
        this.getGoodsList();
      }
    },
    //改变用户状态

    // 删除用户
    async handleDelete(goods_id) {
      // console.log(index);
      // console.log(item);
      //1  判断是否真的要删除  如果 点击取消删除 那么什么都不做 给出取消删除的提示
      // 2 如果是真的要删除  发送ajax 请 求  判断回来的数据 请求成功后 执行删除的提示
      // 3 重新加载页面
      console.log(goods_id)
      let res = await this.$confirm("此操作将永久删除该文件, 是否继续?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).catch(() => {}); // 这里的取消必须加上一个catch 的回调函数
      if (res != "confirm") return this.$message.info("取消删除");
      
      let {
        meta: { status, msg },
      } = await getDeleteGoods({ id: goods_id });

      if (status == 200) {
        this.$message.success("删除成功");
        this.getGoodsList(); // 3 重新加载页面
      } else {
        this.$message.error("删除失败");
      }
    },

    // 编辑用户功能
    // 1 点击 弹出编辑对话框  2 把点击的当前这个一项数据 展示在都对应的input 里面 (通过 id )
    //  从 后台去查询对应的这一条数据
    // 3  修改对应的数据 修改后进行 数据的编辑请求  成功后 关闭对话框,重新刷新页面
    async handleEdit(index, item) {
      // 1 根据id 发送请求
      let res = await getQueryUsersById({ id: item.id });
      let {
        data,
        meta: { status },
      } = res;
      if (status != 200) return this.$message.info("查询失败了");
      this.editeForm = data;
      this.role_id = data.role_id;

      this.editeDialogFormVisible = true;
    },
    // 2 真正的修改完数据后进行数据编辑

    async editUsers() {
      let {
        meta: { status },
      } = await getEditUsers(this.editeForm);
      if (status != 200) return this.$message.info("编辑失败了");

      this.editeDialogFormVisible = false;
      this.getGoodsList();
    },
  },
};
</script>

<style scoped>
.box-card {
  width: 1200px;
}
.userBox {
  padding-left: 20px;
}

.el-table {
  margin-top: 20px;
}

.el-pagination {
  margin: 20px auto;
  text-align: center;
}
</style>


